home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c13 / ToeTest.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  5.0 KB  |  153 lines

  1. //: ToeTest.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Demonstration of dialog boxes
  50. // and creating your own components
  51. import java.awt.*;
  52.  
  53. class ToeButton extends Canvas {
  54.   int state = ToeDialog.BLANK;
  55.   ToeDialog parent;
  56.   ToeButton(ToeDialog parent) {
  57.     this.parent = parent;
  58.   }
  59.   public void paint(Graphics  g) {
  60.     int x1 = 0;
  61.     int y1 = 0;
  62.     int x2 = size().width - 1;
  63.     int y2 = size().height - 1;
  64.     g.drawRect(x1, y1, x2, y2);
  65.     x1 = x2/4;
  66.     y1 = y2/4;
  67.     int wide = x2/2;
  68.     int high = y2/2;
  69.     if(state == ToeDialog.XX) {
  70.       g.drawLine(x1, y1, x1 + wide, y1 + high);
  71.       g.drawLine(x1, y1 + high, x1 + wide, y1);
  72.     }
  73.     if(state == ToeDialog.OO) {
  74.       g.drawOval(x1, y1, x1+wide/2, y1+high/2);
  75.     }
  76.   }
  77.   public boolean 
  78.   mouseDown(Event evt, int x, int y) {
  79.     if(state == ToeDialog.BLANK) {
  80.       state = parent.turn;
  81.       parent.turn= (parent.turn == ToeDialog.XX ?
  82.         ToeDialog.OO : ToeDialog.XX);
  83.     } 
  84.     else
  85.       state = (state == ToeDialog.XX ? 
  86.         ToeDialog.OO : ToeDialog.XX);
  87.     repaint();
  88.     return true;
  89.   }
  90. }
  91.  
  92. class ToeDialog extends Dialog {
  93.   // w = number of cells wide
  94.   // h = number of cells high
  95.   static final int BLANK = 0;
  96.   static final int XX = 1;
  97.   static final int OO = 2;
  98.   int turn = XX; // Start with x's turn
  99.   public ToeDialog(Frame parent, int w, int h) {
  100.     super(parent, "The game itself", false);
  101.     setLayout(new GridLayout(w, h));
  102.     for(int i = 0; i < w * h; i++)
  103.       add(new ToeButton(this));
  104.     resize(w * 50, h * 50);
  105.   }
  106.   public boolean handleEvent(Event evt) {
  107.     if(evt.id == Event.WINDOW_DESTROY) 
  108.       dispose();
  109.     else 
  110.       return super.handleEvent(evt);
  111.     return true;
  112.   }
  113. }
  114.  
  115. public class ToeTest extends Frame {
  116.   TextField rows = new TextField("3");
  117.   TextField cols = new TextField("3");
  118.   public ToeTest() {
  119.     setTitle("Toe Test");
  120.     Panel p = new Panel();
  121.     p.setLayout(new GridLayout(2,2));
  122.     p.add(new Label("Rows", Label.CENTER));
  123.     p.add(rows);
  124.     p.add(new Label("Columns", Label.CENTER));
  125.     p.add(cols);
  126.     add("North", p);
  127.     add("South", new Button("go"));
  128.   }
  129.   public boolean handleEvent(Event evt) {
  130.     if(evt.id == Event.WINDOW_DESTROY) 
  131.       System.exit(0);
  132.     else 
  133.       return super.handleEvent(evt);
  134.     return true;
  135.   }
  136.   public boolean action(Event evt, Object arg) {
  137.     if(arg.equals("go")) {
  138.       Dialog d = new ToeDialog(
  139.         this, 
  140.         Integer.parseInt(rows.getText()),
  141.         Integer.parseInt(cols.getText()));
  142.       d.show();
  143.     } 
  144.     else 
  145.       return super.action(evt, arg);
  146.     return true;
  147.   }
  148.   public static void main(String[] args) {
  149.     Frame f = new ToeTest();
  150.     f.resize(200,100);
  151.     f.show();
  152.   }
  153. } ///:~